package com.xej.seat.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xej.seat.mapper.SeatMapper;
import com.xej.seat.model.SeatingType;
import com.xej.seat.service.CustomerService;
import com.xej.seat.service.SeatService;
import com.xej.seat.service.UserService;
import com.xej.seat.util.DoubleUtil;
import com.xej.seat.vo.Customer;
import com.xej.seat.vo.ManualSeat;
import com.xej.seat.vo.RequestSeat;
import com.xej.seat.vo.Seat;
import com.xej.seat.vo.UserInfo;

/**
 * 后端管理的页面跳转controller
 * lisheng
 */
@Controller
@RequestMapping("/admin")
public class AdminPageController {

    @Resource
    private UserService userService;
    
	@Resource
	private CustomerService customerService;
	
	@Resource
	private SeatService seatService;
	
    @Resource
    private SeatMapper seatMapper;
    
    /**
     * 后台首页
     * @return
     */
    @RequestMapping("/home")
    public String homePage(){
        return "redirect:/admin/seat/list";
    }

    /**
     * 跳转到座位列表页面
     * @return
     */
    @RequestMapping("/seat/list")
    public String articlePage(Model model){
    	UserInfo userInfo = userService.getUserInfo();
        model.addAttribute("userInfo",userInfo);
        return "admin/seat/seatList";
    }
    
    /**跳转到客户页面
     * @return
     */
    @RequestMapping("/customer/list")
    public String partnerPage(Model model){
        UserInfo userInfo = userService.getUserInfo();
        model.addAttribute("userInfo",userInfo);
        return "admin/customer/customerList";
    }

    @RequestMapping("/test")
    public String testPage(){
        return "admin/test";
    }
    
    /**
     * 跳转到排位页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    
    @SuppressWarnings("unlikely-arg-type")
	@RequestMapping("/seatSort")
    @ResponseBody
    public String seatSort(@RequestBody JSONObject requestJson, 
    		HttpServletRequest request) {
    	System.out.println(requestJson.toString());
    	// 规则
    	Integer rule = requestJson.getInteger("rule");
    	//清除相关记录，每次重新生成，目前不考虑多会场
    	this.seatService.deleteAllSeat();
    	// 手动排位的座位
    	JSONArray manualSeatList = requestJson.getJSONArray("manualSeatList");
    	List<ManualSeat> manualSeat = this.seatService.getManualSeatList(manualSeatList);
    	// 需要排位的list
    	JSONArray list = requestJson.getJSONArray("list");
    	List<Seat> jdbclist = new LinkedList<Seat>();
    	int count = 1; // 座位编号
    	for (Object re : list) {
    		RequestSeat rs = ((JSONObject) re).toJavaObject(RequestSeat.class);
			for (int col = 1; col <= rs.getColumns(); col++) {
				Seat seat = new Seat() {
					private static final long serialVersionUID = 1L;
					@Override
					public boolean equals(Object obj) { //定位需手动排的座位
						ManualSeat otherObj = (ManualSeat) obj;
						Integer oRow = otherObj.getRow();
						Integer oColumn = otherObj.getColumn();
						if(oRow.equals(this.getSrow()) && oColumn.equals(this.getScol())) {
							return true;
						}
						return false;
					}
				};
				seat.setSid(count);
				seat.setSrow(rs.getRow());
				//行 权重
				seat.setRuleId(rule);
				seat.setScol(col);
				seat.setHallId(rule.toString());
				seat.setWeight(list.size(), rs.getColumns());
				if(manualSeat.contains(seat)) { //是否手动排座
					seat.setSeatingType(SeatingType.Manual);
				} else {
					seat.setSeatingType(SeatingType.Auto);
				}
				count++;
				jdbclist.add(seat);
			}
		}
    	// 初始化座位
    	seatMapper.initSeat(jdbclist);
    	List<Seat> seats = this.seatService.loadSeatList();
    	
    	//重置所有用户为自动排序
    	this.customerService.updateCustomerAuto();
    	//不需要排位的人员编号
    	JSONArray specificPersonList = requestJson.getJSONArray("specificPersonList");
    	if(specificPersonList != null && !specificPersonList.isEmpty()) {
    		for(Object obj : specificPersonList) {
    			Integer cusId = (Integer) obj;
    			this.customerService.updateCustomerManualById(cusId.toString());
    		}
    	}
    	// 获取排位的人员
    	List<Customer> customers = this.customerService.loadCustomer();
    	this.customerService.setSeatFromCustomer(seats, customers);
    	// 7：将用户对应的放入座位中
    	Map<String,Object> map =  new HashMap<String,Object>();
    	map.put("rule", rule);
    	map.put("list", seats);
    	return JSON.toJSONString(map);
    }
    
}
